package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.Bean.User;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private EditText usnedit, psdedit;
    private Button loginbtn, registerbtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getUser();
        initView();
        setListener();

    }

    private void setListener() {
//        登录按钮
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usn = usnedit.getText().toString().trim();//获取账号密码
                String psd = psdedit.getText().toString().trim();
                User user = confirmUser(usn, psd);
                if (user == null) {
                    //登录失败
                    showToast("账号或者密码错误！");
                } else {
                    showToast("登录成功," + usn + ",欢迎回来！");
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putInt("myid", user.getId());//获取id
                    editor.putString("myname", user.getName());//获取名字
                    editor.putInt("myimg", user.getUserimg());//获取头像
                    editor.apply();
                    startActivity(intent);

                }
            }
        });
//        跳转到注册页面
        registerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent1);

            }

        });
    }

    //账号密码错误弹窗
    private void showToast(String str) {
        Toast.makeText(LoginActivity.this, str, Toast.LENGTH_SHORT).show();

    }

    //判断账号密码
    private User confirmUser(String usn, String psd) {
        List<User> userListr = LitePal.findAll(User.class);
        for (User u : userListr) {
            if (usn.equals(u.getName()) && psd.equals(u.getPassword())) {
                return u;
            }
        }
        return null;
    }

    //    存入数据
    private void getUser() {
        if (LitePal.findAll(User.class).size() == 0) {//判断 ，如果数据中没有数据，则添加一下数据
            new User(0, "lgt", "123", R.mipmap.ailian).save();
            new User(0, "lc", "123", R.mipmap.leimua).save();
            new User(0, "lzy", "123", R.mipmap.head_xiamu).save();
            new User(0, "李光通", "123", R.mipmap.head_yin2).save();
            new User(0, "正焕", "123", R.mipmap.head_zou).save();
        }


    }

    private void initView() {
        usnedit = findViewById(R.id.usnedit);
        psdedit = findViewById(R.id.psdedit);
        loginbtn = findViewById(R.id.loginbtn);
        registerbtn = findViewById(R.id.registerbtn);
    }
}
